﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ZLDC.Common.Estate;

namespace ZLDC.Estate
{
    public class CHandlerCheck
    {
        public static void ChooseMethod()
        {

            string act = _Request.GetString("act", "");

            switch (act)
            {
                #region 楼盘字典别名审核

                case "othername_list":
                    OtherNameList();
                    break;
                case "othername_check":
                    OtherNameCheck();
                    break;
                case "othername_detail":
                    OtherNameDetail();
                    break;

                #endregion

                #region 楼盘字典审核

                case "estate_applylist":
                    EstateApplyList(); break;
                case "estate_checklist":
                    EstateCheckList(); break;
                case "build_applylist": BuildAppLyList();
                    break;
                case "build_checklist":
                    BuildCheckList(); break;
                case "roomno_applylist": RoomNoAppLyList();
                    break;
                case "roomno_checklist":
                    RoomnoCheckList(); break;
                case "estate_check":
                    EstateCheck();
                    break;
                case "EstateCheckApproval_views":
                    EstateCheckApproval_views();
                    break;
                case "build_check":
                    BuildCheck();
                    break;
                case "BuildCheckApproval_views":
                    BuildCheckApproval_views();
                    break;
                case "roomno_check":
                    RoomnoCheck();
                    break;
                case "RoomnoCheckApproval_views":
                    RoomnoCheckApproval_views();
                    break;
                #endregion

                #region 盘源审核

                case "housing_checklist":
                    HousingCheckList();
                    break;
                case "housing_checklist1":
                    HousingCheckList1();
                    break;
                case "housing_check":
                    HousingCheck();
                    break;
                case "housing_checkstatus1":
                    HousingCheckStatus1();
                    break;

                #endregion

            }
        }

        #region 楼盘字典别名审核

        private static void OtherNameList()
        {
            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);

            int typeid = _Request.GetInt("typeid", 1); int isAgreement = _Request.GetInt("isagreement", 0); string startdate = _Request.GetString("startdate", "");
            string enddate = _Request.GetString("enddate", "");
            string name = _Request.GetString("name", "");

            string condition = " where type = " + typeid.ToString();
            if (isAgreement > -1)
                condition += " and isAgreement = " + isAgreement.ToString();
            if (startdate != "" && enddate != "")
                condition += " and commitDate > '" + startdate + "' and commitDate < '" + enddate + "'";
            if (name != "")
                condition += " and modifyName like '%" + name + "%'";

            string json = ZLDC.Topo.BLL.Estate.EstateOtherNameBLL.EstateOtherNameList(page, pagesize, typeid, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void OtherNameCheck()
        {
            int id = _Request.GetInt("id", 0);
            int isAgreement = _Request.GetInt("isagreement", 0); string remark = _Request.GetString("remark", "");


            ZLDC.Common.BaseUser.Param profile = new ZLDC.Common.BaseUser.Param();
            profile = _Utility.GetInfoBySession();

            string agreeName = profile.m_sUserName;
            string agreeDapt = profile.m_sDeptName;

            string outmsg = ZLDC.Topo.BLL.Estate.EstateOtherNameBLL.EstateOtherNameCheck(id, agreeName, agreeDapt, isAgreement, remark);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }

        private static void OtherNameDetail()
        {
            int id = _Request.GetInt("id", 0);

            string outmsg = ZLDC.Topo.BLL.Estate.EstateOtherNameBLL.GetFinalNameByID(id);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }

        #endregion

        #region 楼盘字典审核

        public static void EstateApplyList()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            string userName = _Utility.GetInfoBySession().m_sUserName;
            string station = _Utility.GetInfoBySession().m_sStation;
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);

            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string ename = _Request.GetString("ename", "");

            string condition = " where delFlag=0  and registerUserid=" + userid;
            if (!ename.Equals(""))
                condition += " and name='" + ename + "'";

            string json = ZLDC.Topo.BLL.Estate.EstateBLL.EstateList(userid, page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void EstateCheckList()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            string userName = _Utility.GetInfoBySession().m_sUserName;
            string station = _Utility.GetInfoBySession().m_sStation;
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);

            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string ename = _Request.GetString("ename", "");
            string condition = " where delFlag=0 and isValid=1 ";
            if (!ename.Equals(""))
                condition += " and name='" + ename + "'";

            if (!station.Contains("数据服务部") && !station.Contains("专员"))
                condition += " and " + CacheUtility.Cache_GetAreaByDeptId2(deptid);

            string json = ZLDC.Topo.BLL.Estate.EstateBLL.EstateList(userid, page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void BuildCheckList()
        {
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
            string station = _Utility.GetInfoBySession().m_sStation;

            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string ename = _Request.GetString("ename", "");
            string condition = " where delFlag=0 and isValid=1 ";
            if (!ename.Equals(""))
                condition += " and ename='" + ename + "'";

            if (!station.Contains("数据服务部") && !station.Contains("专员"))
                condition += " and " + CacheUtility.Cache_GetAreaByDeptId2(deptid);

            string json = ZLDC.Topo.BLL.Estate.BuildBLL.p_v_build_list_table(page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void RoomnoCheckList()
        {
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
            string station = _Utility.GetInfoBySession().m_sStation;

            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string ename = _Request.GetString("ename", "");
            string condition = " where delFlag=0 and isValid=1 ";
            if (!ename.Equals(""))
                condition += " and finalname like '" + ename + "%'";

            if (!station.Contains("数据服务部") && !station.Contains("专员"))
                condition += " and " + CacheUtility.Cache_GetAreaByDeptId2(deptid);

            string json = ZLDC.Topo.BLL.Estate.RoomNoBLL.p_v_roomno_list_table(page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void EstateCheck()
        {
            int id = 0;
            string outmsg = "";
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("id"), '|');
            int companyId = _Convert.ToInt(_Utility.GetWebConfigSetting("companyId"), 1);
            if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    id = _Convert.ToInt(IDList[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.EstateBLL.EstateCheck(id, companyId);
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }
        private static void EstateCheckApproval_views()
        {
            int id = 0;
            string outmsg = "";
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("id"), '|');
            int isvalid = _Request.GetInt("isvalid", 2);
            string Approval_views = _Request.GetString("Approval_views", ""); if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    id = _Convert.ToInt(IDList[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.EstateBLL.EstateCheck(id, isvalid, Approval_views);
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }
        private static void BuildCheck()
        {
            int id = 0;
            string outmsg = "";
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("id"), '|');

            if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    id = _Convert.ToInt(IDList[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.BuildBLL.BuildCheck(id);
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }
        private static void BuildCheckApproval_views()
        {
            int id = 0;
            string outmsg = "";
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("id"), '|');

            int isvalid = _Request.GetInt("isvalid", 2);
            string Approval_views = _Request.GetString("Approval_views", ""); if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    id = _Convert.ToInt(IDList[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.BuildBLL.BuildCheck(id, isvalid, Approval_views);
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }

        private static void RoomnoCheck()
        {
            int id = 0;
            string outmsg = "";
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("id"), '|');

            if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    id = _Convert.ToInt(IDList[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.RoomNoBLL.RoomnoCheck(id);
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }
        private static void RoomnoCheckApproval_views()
        {
            int id = 0;
            string outmsg = "";
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("id"), '|');
            int isvalid = _Request.GetInt("isvalid", 2);
            string Approval_views = _Request.GetString("Approval_views", ""); if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    id = _Convert.ToInt(IDList[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.RoomNoBLL.RoomnoCheck(id, isvalid, Approval_views);
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }
        #endregion

        #region 盘源审核

        public static void HousingCheckList()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID);
            string deptid = _Utility.GetInfoBySession().m_sDeptID;
            string deptid_list = CacheUtility.Cache_GetAllChildDept(deptid);

            string station = _Utility.GetInfoBySession().m_sStation;

            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            int itemvalue = ZLDC.Topo.BLL.Estate.SystemOptionBLL.GetOption("盘源审核有效时间");

            int typeid = _Request.GetInt("typeid", 0);
            bool flag = _Request.GetBool("flag", false);
            string condition = "";
            if (flag == true)
            {
                condition = GetSelectCondition(deptid_list, station, typeid, itemvalue, flag);
            }
            else
            {
                condition = GetSelectCondition(deptid_list, station, typeid, itemvalue);

            }
            string orderByName = _Request.GetString("orderByName", "");
            string orderByType = _Request.GetString("orderByType", "desc");

            string json = "";
            int id = _Request.GetInt("id", 0); if (id > 0)
            {
                condition += " and id=" + id;

                json = ZLDC.Topo.BLL.Estate.HousingBLL.p_housing_list_check(userid, page, pagesize, condition, orderByName, orderByType);
                HttpContext.Current.Response.Write(json);
            }

            string buildName = _Request.GetString("name", ""); if (buildName != "" && buildName != "楼盘名称、拼音均可")
            {
                string[] buildList = _Utility.GetStringBySplit(buildName, ',');
                condition += " and (";
                for (int i = 0; i < buildList.Length; i++)
                {
                    string item = buildList[i];

                    if (!item.Equals(""))
                    {
                        if (i == 0)
                            condition += " buildName like '%" + item + "%'";
                        else
                            condition += " or buildName like '%" + item + "%'";
                    }
                }
                condition += " )";
            }

            string roomno = _Request.GetString("roomno", ""); if (roomno != "")
                condition += " and roomno='" + roomno + "'";





            json = ZLDC.Topo.BLL.Estate.HousingBLL.p_housing_list_check(userid, page, pagesize, condition, orderByName, orderByType);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static string GetSelectCondition(string deptid_list, string station, int typeid, int itemvalue)
        {
            string dateColumn = "markDate";
            string condition = " where dltFlag=0 ";

            if (typeid == 1)
                condition += " and isValid = 1";
            else if (typeid == 2)
            {
                dateColumn = "modifyDate";
                condition += " and status1 > 1";
            }

            if (station.Contains("分行经理") || station.Contains("业务主任"))
            {
                condition += "  and markUdeptid in(" + deptid_list + ")";
            }
            else if (station.Contains("区域经理") || station.Contains("分区经理") || station.Contains("营运副总监") || station.Contains("营运总监"))
            {
                condition += " and markUdeptid in(" + deptid_list + ")";
            }
            else if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("查看全部盘源"))
            {
                condition += "";
            }
            else
                condition += " and markUid=" + _Utility.GetInfoBySession().m_sUserID + "";
            return condition;
        }
        private static string GetSelectCondition(string deptid_list, string station, int typeid, int itemvalue, bool flag)
        {
            string dateColumn = "markDate";
            string condition = " where dltFlag=0 ";

            if (typeid == 1)
                condition += " and isValid = 1";
            else if (typeid == 2)
            {
                dateColumn = "modifyDate";
                condition += " and status1 > 1";
            }
            if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("查看全部盘源"))
            {
                condition += "";
            }
            else
            {
                condition += " and (datediff(hour, " + dateColumn + ", GETDATE()) <= " + itemvalue + " or datediff(hour, modifyDate, GETDATE()) <= " + itemvalue + ") and markUdeptid in(" + deptid_list + ")";
            }
            return condition;
        }

        public static void HousingCheckList1()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID);
            string deptid = _Utility.GetInfoBySession().m_sDeptID;
            string deptid_list = CacheUtility.Cache_GetAllChildDept(deptid);
            string station = _Utility.GetInfoBySession().m_sStation;

            int typeid = _Request.GetInt("typeid", 0); int pagesize = _Request.GetInt("pagesize", 0);
            int itemvalue = ZLDC.Topo.BLL.Estate.SystemOptionBLL.GetOption("盘源审核有效时间");

            string condition = GetSelectCondition(deptid_list, station, typeid, itemvalue);

            string json = ZLDC.Topo.BLL.Estate.HousingBLL.SelectMyHousing(pagesize, condition.Replace("where", ""));

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void HousingCheck()
        {
            int id = _Request.GetInt("id", 0);
            int status = _Request.GetInt("status", 0);
            string json = ZLDC.Topo.BLL.Estate.HousingBLL.HousingCheck(id, status);

            if (id > 0)
            {
                _Cache.Remove(CacheConst.CACHE_HOUSING_DETAIL_JSON + id.ToString());
                _Cache.Remove(CacheConst.CACHE_HOUSING_DETAIL_INFO + id.ToString());
                _Cache.RemoveLike(CacheConst.CACHE_HOUSING_FOLLOW_JSON + id);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void HousingCheckStatus1()
        {
            int id = _Request.GetInt("id", 0);
            int status = _Request.GetInt("status", 0);
            string json = ZLDC.Topo.BLL.Estate.HousingBLL.HousingCheckStatus1(id, status);

            if (id > 0)
            {
                _Cache.Remove(CacheConst.CACHE_HOUSING_DETAIL_JSON + id.ToString());
                _Cache.Remove(CacheConst.CACHE_HOUSING_DETAIL_INFO + id.ToString());
                _Cache.RemoveLike(CacheConst.CACHE_HOUSING_FOLLOW_JSON + id);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void BuildAppLyList()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            string userName = _Utility.GetInfoBySession().m_sUserName;
            string station = _Utility.GetInfoBySession().m_sStation;
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);

            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string ename = _Request.GetString("ename", "");
            string condition = " where delFlag=0  and registerid=" + userid;
            if (!ename.Equals(""))
                condition += " and ename='" + ename + "'";

            string json = ZLDC.Topo.BLL.Estate.BuildBLL.BuildList(page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void RoomNoAppLyList()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            string userName = _Utility.GetInfoBySession().m_sUserName;
            string station = _Utility.GetInfoBySession().m_sStation;
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);

            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string ename = _Request.GetString("ename", "");
            string condition = " where delFlag=0  and registerid=" + userid;
            if (!ename.Equals(""))
                condition += " and finalname like'" + ename + "%'";

            string json = ZLDC.Topo.BLL.Estate.RoomNoBLL.RoomNoList(page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }
        #endregion
    }
}
